/**
 * @author Vitor Franco do Carmo (vitor.carmo@capgemini.com)
 * @since 18/04/2013
 *
 */
package br.com.vfcfw.controller.converter;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;

import br.com.vfcfw.commons.domain.enums.ESimNao;

/**
 * Converter para a enumeração {@link ESimNao}.
 * 
 * @author Vitor Franco do Carmo (vitor.carmo@capgemini.com)
 * @since 18/04/2013
 * 
 */
@FacesConverter(value = "simNaoConverter")
@ManagedBean(name = "simNaoConverter", eager = true)
@ApplicationScoped
public class SimNaoConverter extends BaseConverter {

    /**
     * {@inheritDoc}
     * 
     * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
     *      javax.faces.component.UIComponent, java.lang.String)
     */
    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {

        Character retorno = null;

        if (obterMensagem(ESimNao.SIM.getChaveDescricao()).equals(value)) {

            retorno = ESimNao.SIM.getValor();
        } else if (obterMensagem(ESimNao.NAO.getChaveDescricao()).equals(value)) {

            retorno = ESimNao.NAO.getValor();
        }
        return retorno;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
     *      javax.faces.component.UIComponent, java.lang.Object)
     */
    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {

        String retorno = null;

        if (possuiValor(value)) {

            ESimNao simNao = ESimNao.obterPorValor((Character) value);

            retorno = obterMensagem(simNao.getChaveDescricao());
        }
        return retorno;
    }

}
